
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menuLogin.java
 *
 * Created on 10/08/2011, 15:09:19
 */
/**
 *
 * @author Anthony
 */
public class menuLogin extends javax.swing.JDialog {

    /** Creates new form menuLogin */
    public menuLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        JLabel1 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoSenha = new javax.swing.JPasswordField();
        botaoSair = new javax.swing.JButton();
        botaoLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login - SisTeca V 1.0.1");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Franklin Gothic Medium", 1, 14));
        jLabel3.setText("SisTeca V 1.0.1  Powered By SkyNet");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 0, -1, -1));

        JLabel1.setBackground(new java.awt.Color(102, 255, 0));
        JLabel1.setFont(new java.awt.Font("Lucida Sans Typewriter", 1, 14));
        JLabel1.setText("Nome");
        jPanel2.add(JLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 110, 40, 23));

        campoNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoNomeActionPerformed(evt);
            }
        });
        campoNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoNomeKeyPressed(evt);
            }
        });
        jPanel2.add(campoNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 110, 120, 23));

        jLabel2.setFont(new java.awt.Font("Lucida Sans Typewriter", 1, 14));
        jLabel2.setText("Senha");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, 40, 23));
        jPanel2.add(campoSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 140, 120, 23));

        botaoSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        botaoSair.setText("Sair");
        botaoSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSairActionPerformed(evt);
            }
        });
        jPanel2.add(botaoSair, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 240, 140, -1));

        botaoLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        botaoLogin.setText("login");
        botaoLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoLoginActionPerformed(evt);
            }
        });
        jPanel2.add(botaoLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 240, 130, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/key.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, -40, 360, 300));

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoLoginActionPerformed
        try{ 
           Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String nome = campoNome.getText();
            String senha = campoSenha.getText();
            String sql = "select nome, senha from funcionario where nome = ? and senha = ?";
            
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, nome);
            query.setString(2, senha);
            ResultSet rs = query.executeQuery();
             
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "autenticado com sucesso."); 
                menuPrincipal a = new menuPrincipal();
                a.show();
                this.dispose();
            }
            else{
                 if(campoNome.getText().equals("") || campoSenha.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Campo(s) em branco!"); 
                }
                 else{
               
                    JOptionPane.showMessageDialog(null, "Nome ou senha invalido"); 
                 }
            }
        query.close();
        rs.close();
           
        } catch(Exception e){
            System.out.println("Deu erro...");
            e.printStackTrace();
        }
       
      
        
    }//GEN-LAST:event_botaoLoginActionPerformed

    private void campoNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoNomeActionPerformed
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_campoNomeActionPerformed

    private void botaoSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSairActionPerformed
        // TODO add your handling code here:
       if(JOptionPane.showConfirmDialog(this, "Deseja realmente sair?", "Encerrar programa", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
				this.dispose();
	}
    }//GEN-LAST:event_botaoSairActionPerformed

    private void campoNomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoNomeKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoNomeKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                menuLogin dialog = new menuLogin(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JLabel1;
    private javax.swing.JButton botaoLogin;
    private javax.swing.JButton botaoSair;
    private javax.swing.JTextField campoNome;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

    
    }

